#region

using System.Collections.Generic;
using DomainModel.Domain.Model;
using DomainModel.Domain.Model.MessageAssemblyModel;
using DomainModel.Domain.Model.ServiceModel;
using DomainModel.Utils;
using NHibernate;
using NHibernate.Criterion;

#endregion

namespace DomainModel.Infrastructure.Persistence
{
    public class ServicesRepository : BaseRepository<Service, ServiceId>, IServicesRepository
    {
        public ServicesRepository(ISession session, IApplicationConfig applicationConfig)
            : base(session, applicationConfig)
        {
        }

        public IList<Service> Get(MessageAssembly messageAssembly)
        {
            IQuery q = Session.CreateQuery("from Service as s where (:messageAssembly) in elements(s.MessageAssembliesSet)");
            q.SetParameter("messageAssembly", messageAssembly);


            return q.List<Service>();
        }      

        public Service Get(QueueInfo inputQueue)
        {
            var service = BuildCriteria()
                .Add(Restrictions.Eq("InputQueue", inputQueue))
                .UniqueResult<Service>();

            if (service != null && service.MessageAssembliesSet != null)
            {
                foreach (var m in service.MessageAssembliesSet)
                {
                    if (m.MessageTypesList != null)
                    {
                        var x = m.MessageTypesList.Count;
                    }
                }
            }

            return service;
        }

        public ServiceId NextServiceId()
        {
            return new ServiceId(NextIntFromSeq());
        }

        public ServiceRoleId NextServiceRoleId()
        {
            return new ServiceRoleId(NextIntFromSeq());
        }
    }
}